package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.Attach;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.service.AttachService;
import com.heitian.ssm.utils.CheckUtil;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/18 下午1:13
 * 版本： [1.0, 17/10/18]
 * 描述： <描述>
 */
@Controller
@RequestMapping("/file")
public class AttachController {

    private Logger log = Logger.getLogger(AttachController.class);

    @Resource
    AttachService attachService;

    /**
     * 保存文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/SaveAttach", method = RequestMethod.POST)
    public void SaveAttach(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        Attach attach = new Attach();
        if (paras.has("AttachName")) {
            attach.setAttachName(paras.get("AttachName").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("AttachLength")) {
            attach.setAttachLength(paras.get("AttachLength").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("AttachUrl")) {
            attach.setAttachUrl(paras.get("AttachUrl").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("OwnerUserGuid")) {
            attach.setOwnerUserGuid(paras.get("OwnerUserGuid").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("AlbumGuid")) {
            attach.setAlbumGuid(paras.get("AlbumGuid").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("AttachType")) {
            attach.setAttachType(paras.get("AttachType").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        attach.setAttachGuid(CommonUtils.GetUUID());
        attach.setUpdataTime(CommonUtils.getCurrentTime());
        attach.setCategoryType("img");
        attach.setAttachState("0");
        attachService.SaveAttach(attach);
        response.getWriter().write(CommonUtils.createJson("1", "添加成功！", null).toString());
    }

    /**
     * 删除文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/DeleteAttach", method = RequestMethod.POST)
    public void DeleteAttach(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AttachGuids = paras.get("AttachGuids").getAsString();
        String[] attachGuids = AttachGuids.split(";");
        for (int i = 0; i < attachGuids.length; i++) {
            attachService.DeleteAttach(attachGuids[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除成功！", null).toString());
    }

    /**
     * 获取相册下的附件数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllAttachCountByAlbumGuid", method = RequestMethod.POST)
    public void getAllAttachCountByAlbumGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AlbumGuid = paras.get("AlbumGuid").getAsString();
        int count = attachService.getAllAttachCountByAlbumGuid(AlbumGuid);
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

    /**
     * 获取文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAttachByGuid", method = RequestMethod.POST)
    public void getAttachByGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AttachGuid = paras.get("AttachGuid").getAsString();
        Attach attach = attachService.getAttachByGuid(AttachGuid);
        String UserArea = null;
        String code = "0";
        String msg = "";
        if (attach != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(attach);
        }else{
            msg = "未找到文件";
        }
        response.getWriter().write(CommonUtils.createJson(code, msg, UserArea).toString());
    }

    /**
     * 分页获取文件数据
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAttachByAlbumGuid", method = RequestMethod.POST)
    public void getAttachByAlbumGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String AlbumGuid = paras.get("AlbumGuid").getAsString();
        String index = paras.get("index").getAsString();
        String currentPageCount = paras.get("currentPageCount").getAsString();
        PageBean<Attach> pageBean = attachService.getAttachByAlbumGuid(AlbumGuid,Integer.parseInt(index),Integer.parseInt(currentPageCount));
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 获取文件所有数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllAttachCount", method = RequestMethod.POST)
    public void getAllAttachCount(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        int count = attachService.getAllAttachCount();
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

}
